//
//  Filter.m
//  JinliWallet
//
//  Created by 张玮 on 2018/10/17.
//  Copyright © 2018年 张玮. All rights reserved.
//

#import "Filter.h"

//#import "LoginViewController.h"
//#import "RegisterVC.h"
//#import "MineVC.h"
//#import "HomePage.h"

@class BaseViewController;

@implementation Filter


+ (void)filterWithServerCode:(ResponseClass *)response
              withItem:(id)item
              withPassBlock:(void (^)(id paginationObj , id resonseObj ,BOOL isSuccess,NSString *message))block{
    if (response.code == RESPONSE_CODE_SUCCESS){
        block(response.pagination , item , YES ,response.message);
    }else if (response.code == RESPONSE_CODE_TOKEN_INVALID ||
              response.code == RESPONSE_CODE_NO_USER ||
              response.code == RESPONSE_CODE_TOKEN_FAILER||
              response.code == RESPONSE_CODE_UNREGISTER){
        // TODO: 处理token 过期
        [MSNetworkManager showLoginAlert];//跳转登录页面重新登录
        [[MSStorageUtils shareInstance] userDefaultSave:@"" withKey:USER_DEFAULT_KEY_MEMBER_TOKEN];//超时删除token
        block(response.pagination , nil , NO , nil);
    } else {
        [[MSUtils shareInstance] showMessage:response.message];
        block(response.pagination , response.data , NO ,response.message);
    }
}

+ (void)filterWithHttpCode:(NSError *)error withPassBlock:(void (^)(NSUInteger code, NSString *message))block
{
    if (block) {
        block((NSInteger)(labs(error.code)) , @"");
    }
    else
    {
        block = nil;
    }
}

//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

@end
